{% extends "base.html" %}
{% block title %}注册{% endblock %}
{% block css %}
    .form-signin {
    max-width: 400px;
    padding: 15px;
    margin: 0 auto;
    }
{% endblock %}

{% block content %}
    <div class="bs-example" data-example-id="panel-with-list-group">
        <!-- Default panel contents -->
        <div class="panel-heading"><h3 style="text-align: center">注册</h3></div>
        <div class="panel-body">
            <form class="form-signin" method="post">
                <div class="form-group">
                    <label for="email">邮箱：</label>
                    <input type="email" class="form-control" name="email" id="email" placeholder="Email">
                </div>
                <div class="form-group">
                    <label for="captcha">验证码：</label>
                    <div class="input-group">
                        <input type="text" id="captcha" name="captcha" class="form-control" placeholder="captcha">
                        <span class="input-group-btn">
            <button class="btn btn-primary" id="get_captcha" type="button">获取验证码</button>
          </span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" name="username" class="form-control" id="username" placeholder="username">
                </div>
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" name="password" class="form-control" id="password" placeholder="password">
                </div>
                <div class="form-group">
                    <label for="password_confirm">确认密码：</label>
                    <input type="password" name="password_confirm" class="form-control" id="password_confirm"
                           placeholder="password_confirm">
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    </div>


{% endblock %}
{% block js %}
    <script type="text/javascript">

        $(function () {
            bindEmailCaptchaClick()
        })

        function bindEmailCaptchaClick() {
            $("#get_captcha").click(function (event) {
                var $this = $(this)
                // 阻止表单提交
                event.preventDefault()
                const email = $("input[name='email']").val()
                $.ajax({
                    url: "/auth/captcha/email?email=" + email, // 请求地址,
                    method: "POST",
                    success: function (result) {
                        var code = result.code
                        if (code === 200) {
                            var countdown = 60
                            // 开始倒计时之前，就取消按钮的点击事件
                            $this.off("click")
                            var timer = setInterval(function () {
                                $this.text(countdown + "s后重试")
                                countdown--
                                // 倒计时结束的时候执行
                                if (countdown === 0) {
                                    clearInterval(timer)
                                    $this.text("获取验证码")
                                    bindEmailCaptchaClick()
                                }
                            }, 1000)
                        }
                    },
                    fail: function (error) {
                        console.log(error)
                    }
                })
            })
        }
    </script>
{% endblock %}

